<template>
  <div class="divBox">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <el-button  type="primary" class="mr10" @click="add" size="small" v-hasPermi="['admin:system:user:level:save']">添加用户等级</el-button>
      </div>
      <el-table
        v-loading="listLoading"
        :data="tableData.data"
        style="width: 100%"
        size="mini"
      >
        <el-table-column
          prop="id"
          label="ID"
          min-width="50"
        />
        <el-table-column label="等级图标" min-width="80">
          <template slot-scope="scope">
            <div class="demo-image__preview">
              <el-image
                style="width: 36px; height: 36px"
                :src="scope.row.icon"
                :preview-src-list="[scope.row.icon]"
              />
            </div>
          </template>
        </el-table-column>
        <el-table-column
          prop="name"
          label="等级名称"
          min-width="100"
        />
        <el-table-column
          prop="experience"
          label="经验"
          min-width="100"
        />
        <el-table-column
          prop="discount"
          label="享受折扣(%)"
          min-width="100"
        />
        <el-table-column
          label="状态"
          min-width="100"
        >
          <template slot-scope="scope" v-if="checkPermi(['admin:system:user:level:use'])">
            <el-switch
              v-model="scope.row.isShow"
              :active-value="true"
              :inactive-value="false"
              active-text="开启"
              inactive-text="关闭"
              disabled
              @click.native="onchangeIsShow(scope.row)"
            />
          </template>
        </el-table-column>
        <el-table-column label="操作" min-width="120" fixed="right" align="center">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="edit(scope.row)" class="mr10" v-hasPermi="['admin:system:user:level:update']">编辑</el-button>
            <el-button type="text" size="small" @click="handleDelete(scope.row.id, scope.$index)" v-hasPermi="['admin:system:user:level:delete']">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <creat-grade ref="grades" :user="userInfo"></creat-grade>
  </div>
</template>

<script>
  import { userListApi, groupListApi, levelListApi, levelUseApi, levelDeleteApi } from '@/api/user'
  import creatGrade from './creatGrade'
  import { checkPermi } from "@/utils/permission"; // 权限判断函数
  export default {
    name: 'Grade',
    filters: {
      typeFilter(status) {
        const statusMap = {
          'wechat': '微信用户',
          'routine': '小程序你用户',
          'h5': 'H5用户'
        }
        return statusMap[status]
      }
    },
    components: {creatGrade},
    data() {
      return {
        listLoading: true,
        userInfo:{},
        tableData: {
          data: [],
          total: 0,
        }
      }
    },
    mounted() {
      this.getList()
    },
    methods: {
      checkPermi,
      seachList() {
        this.getList()
      },
      add() {
        this.$refs.grades.dialogVisible = true
        this.userInfo = {};
      },
      edit(id) {
        // this.$refs.grades.info(id)
        this.userInfo = id;
        this.$refs.grades.dialogVisible = true
      },
      // 列表
      getList() {
        this.listLoading = true
        //levelListApi().then(res => {
        //后期接口调用
        let res = [{"id":1,"name":"普通会员","experience":1,"isShow":true,"grade":1,"discount":100,"icon":"https://api.java.crmeb.net/crmebimage/public/maintain/2021/10/13/ce34f7cf454846a28b152049ba5494417vok0wvl7l.png","isDel":false,"updateTime":"2021-11-09 16:38:53","createTime":"2021-09-01 11:29:20"},{"id":2,"name":"黄铜会员","experience":500,"isShow":true,"grade":2,"discount":90,"icon":"https://api.java.crmeb.net/crmebimage/user/2021/08/09/8b41cacd284e4da8a01b6c139716eaa0f5nunj66l4.png","isDel":false,"updateTime":"2021-09-29 16:41:37","createTime":"2021-09-09 15:02:43"},{"id":3,"name":"白银会员","experience":999,"isShow":true,"grade":3,"discount":85,"icon":"https://api.java.crmeb.net/crmebimage/public/maintain/2021/10/13/197514463aad4dbf88cd4266ad35254a18dwgv0dt8.png","isDel":false,"updateTime":"2021-11-09 16:39:24","createTime":"2021-09-14 16:07:23"},{"id":5,"name":"黄金会员","experience":10000,"isShow":true,"grade":4,"discount":80,"icon":"https://api.java.crmeb.net/crmebimage/public/maintain/2021/10/13/0b832422ad3b4987b15ab10b3009a551cu8tccg6ej.png","isDel":false,"updateTime":"2021-11-09 16:39:36","createTime":"2021-09-27 09:59:20"},{"id":4,"name":"钻石会员","experience":12000,"isShow":true,"grade":6,"discount":70,"icon":"https://api.java.crmeb.net/crmebimage/public/maintain/2021/10/13/703948f0db5a453f8be387323fe82f80wyw14ya38y.png","isDel":false,"updateTime":"2022-08-12 13:47:05","createTime":"2021-09-14 16:30:20"},{"id":12,"name":"超级黑金会员","experience":999999,"isShow":true,"grade":7,"discount":3,"icon":"https://api.java.crmeb.net/crmebimage/public/content/2022/08/12/80723379d454425d8bfb17aee5fe3aaax4xcsz5owu.png","isDel":false,"updateTime":"2022-08-15 20:04:59","createTime":"2022-08-15 20:04:59"},{"id":14,"name":"werwer","experience":1000000,"isShow":false,"grade":8,"discount":6,"icon":"https://api.java.crmeb.net/crmebimage/public/content/2022/08/26/bfcfa6e8255c48279268bf887d316204sum1gm82im.png","isDel":false,"updateTime":"2022-08-26 14:30:07","createTime":"2022-08-26 14:30:07"},{"id":13,"name":"地方","experience":10000000,"isShow":false,"grade":10,"discount":1,"icon":"https://api.java.crmeb.net/crmebimage/public/maintain/2022/08/19/6b1c504ee1b34315a043421189d24b1b8jzjli4ixj.jpg","isDel":false,"updateTime":"2022-08-20 10:24:21","createTime":"2022-08-20 10:24:21"},{"id":15,"name":"超级大会员","experience":100000000,"isShow":false,"grade":10000,"discount":1,"icon":"https://api.java.crmeb.net/crmebimage/public/maintain/2022/10/14/93d36bc1ee3c4deebde58d5d8e9e7c39u7y1mgcp9i.jpg","isDel":false,"updateTime":"2022-10-18 09:48:55","createTime":"2022-10-18 09:48:55"}];

          this.tableData.data = res
          this.listLoading = false
        /*}).catch(() => {
          this.listLoading = false
        })*/
      },
      // 删除
      handleDelete(id, idx) {
        this.$modalSure('删除吗？删除会导致对应用户等级数据清空，请谨慎操作！').then(() => {
          levelDeleteApi(id).then(() => {
            this.$message.success('删除成功')
            this.tableData.data.splice(idx, 1)
          })
        })
      },
      onchangeIsShow(row) {
        if(row.isShow == false){
          row.isShow = !row.isShow
          levelUseApi({id: row.id, isShow:row.isShow}).then(() => {
            this.$message.success('修改成功')
            this.getList()
          }).catch(()=>{
            row.isShow = !row.isShow
          })
        }else{
          this.$modalSure('该操作会导致对应用户等级隐藏，请谨慎操作').then(() => {
            row.isShow = !row.isShow
            levelUseApi({id: row.id, isShow:row.isShow}).then(() => {
              this.$message.success('修改成功')
              this.getList()
            }).catch(()=>{
              row.isShow = !row.isShow
            })
          })
        }
      }
    }
  }
</script>

<style scoped lang="scss">
  .el-switch.is-disabled {
    opacity: 1;
  }
  ::v-deep .el-switch__label {
    cursor: pointer !important;;
  }
</style>
